package com.hmall.cart.listener;

import com.hmall.cart.service.ICartService;
import com.hmall.common.constants.MqConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.Map;

@Component
@RequiredArgsConstructor
public class CartListener {
        private  final ICartService cartService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.TRADE_SUCCESS_QUEUE,durable = "true"),
            exchange = @Exchange(value = MqConstants.TRADE_EXCHANGE_NAME,type ="topic"),
            key =MqConstants.TRADE_SUCCESS_KEY
    ))
    public void deleteCartItemByIds(Map<String,Object> map){
        //long uid = (Integer) map.get("uid");
        long uid = Long.parseLong(map.get("uid").toString());
        Collection<Long> itemIds = (Collection<Long>) map.get("itemIds");
        cartService.removeByItemIds(uid,itemIds);
    }
}
